package com.hj.config.security.cf;

import com.hj.config.security.filter.PwdLoginFilter;
import com.hj.config.security.provider.PwdLoginProvider;
import com.hj.util.CypherUtil;
import com.hj.util.RedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * @Description 密码登录配置
 * @author hj
 */
@Configuration
public class PwdLoginConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {


    @Value("${enroll.pwdLoginUri}")
    private String pwdLoginUri;
    @Value("${enroll.pwdMobile}")
    private String pwdMobile;
    @Value("${enroll.pwdPassword}")
    private String pwdPassword;
    @Value("${enroll.pwdVerifyCode}")
    private String pwdVerifyCode;
    @Value("${enroll.httpMethod}")
    private String httpMethod;

    @Resource
    private UserDetailsService userDetailsServiceImpl;
    @Resource
    private AuthenticationSuccessHandler authSuccessHandler;
    @Resource
    private AuthenticationFailureHandler customAuthenticationFailureHandler;
    @Resource
    private CypherUtil cypherUtil;
    @Resource
    private RedisUtil redisUtil;


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(HttpSecurity builder) {
        PwdLoginFilter filter = new PwdLoginFilter(pwdLoginUri, pwdMobile, pwdPassword, pwdVerifyCode, httpMethod);
        filter.setAuthenticationManager(builder.getSharedObject(AuthenticationManager.class));
        filter.setAuthenticationSuccessHandler(authSuccessHandler);
        filter.setAuthenticationFailureHandler(customAuthenticationFailureHandler);
        PwdLoginProvider pwdAuthProvider = new PwdLoginProvider();
        pwdAuthProvider.setUserDetailsServiceImpl(userDetailsServiceImpl);
        pwdAuthProvider.setPasswordEncoder(passwordEncoder());
        pwdAuthProvider.setCypherUtil(cypherUtil);
        pwdAuthProvider.setRedisUtil(redisUtil);
        builder.authenticationProvider(pwdAuthProvider)
                .addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class);
    }


}
